package org.csu.mypetstore.persistence.impl;

import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.persistence.BaseDao;
import org.csu.mypetstore.persistence.SearchDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SearchDaoImpl extends BaseDao implements SearchDao {
    @Override
    public List<Product> getAllProduct() {
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        List<Product> products = new ArrayList<>();
        try {
            conn = getConnection();
            String sql = "select * from product";
            psmt = conn.prepareStatement(sql);
            rs = executeQuery(psmt);
            if(rs != null){
                while (rs.next()){
                    Product product = new Product();
                    product.setName(rs.getString("Name"));
                    product.setProductId(rs.getString("ProductId"));
                    products.add(product);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return products;
    }
}
